package clock;

import input.IInput;

import java.util.Date;

public class Clock implements IInput {

    // offset in x*60000
    private int usersetting = 0;
    // MODES: 0 (IDLE), 1 (DISPLAY), 2 (SETMINUTES), 3 (SETHOURS)
    private byte mode = 0;

    public Clock() { }

    public String currentTime() {
        String current = String.valueOf(new Date(System.currentTimeMillis() + (this.usersetting * 60000)));
        switch (mode) {
            case 0:
                return "";
            case 1:
                break;
            case 2:
                current += " [SETTING MINUTES]";
                break;
            case 3:
                current += " [SETTING HOURS]";
        }
        return current;
    }

    public void button1() {
        this.cycleUsersetting();
    }

    public void button2() {
        this.cycleMode();
    }

    private void cycleMode() {
        if (mode == 3) {
            this.mode = 1;
            return;
        }
        this.mode += 1;
    }

    private void cycleUsersetting() {
        // stores how much the usersetting for the current time is increasing
        // dependent on mode
        int add = 0;
        switch (mode) {
            // in mode IDLE and DISPLAY, time can't be set
            case 0:
                this.mode = 1;
                return;
            case 1:
                this.mode = 0;
                return;
            // we're on mode SETMINUTES, so increase by 1 minute
            case 2:
                add = 1;
                break;
            // we're on mode SETHOURS, so increase by 60 minutes
            case 3:
                add = 60;
                break;
        }

        // if usersetting spans over an entire day (60*24=1440), start counting
        //  at 0 again
        if (this.usersetting + add > 1440) {
            this.usersetting = (this.usersetting + add) % 1440;
            return;
        }

        this.usersetting += add;
    }
}
